import { Component, OnInit, Input } from '@angular/core';

import { AlertController } from '@ionic/angular';

import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';

@Component({
  selector: 'app-registered-list',
  templateUrl: './registered-list.component.html',
  styleUrls: ['./registered-list.component.scss'],
})
export class RegisteredListComponent implements OnInit {
  @Input() tagData: any;
  listData = [];
  pageNo = 0;

  content = {
    pageNo: 1,
    pageSize: 10,
  };
  constructor(
    private appGlobalService: AppGlobalService,
    private http: HttpService,
    public alertController: AlertController
    ) { }

  ngOnInit() {
    this.listData = this.tagData.data.data;
    this.pageNo = this.tagData.data.page;
    // 刷新上一页面
    localStorage.setItem('needRefreshTab1', 'true');
  }
  // 页面跳转
  go(item) {
    const pageParams = {
      viewId: item.target,
      params: item.params,
      sessionId: localStorage.getItem('sessionId')
    };
    console.log(pageParams);
    this.appGlobalService.goToForward(pageParams);
  }
  // 请求数据
  submit(item, index, obj) {
    const phone = sessionStorage.getItem('phone') || null;
    const params = {
      content: item.submit.content,
      id: item.submit.id,
      sessionId: localStorage.getItem("sessionId")
    };
    // 请求获取新数据
    this.http.post(
      "application/submit",
      params,
      res => {
        if (res.success) {
          item.disable = true;
          this.listData.forEach( (v, i) => {
            if (i !== index) {
              v.textBottom.map( k => {
                if (k.submit && phone === v.title) {
                  k.disable = false;
                }
              });
            }
          });
          sessionStorage.setItem('phone', obj.title);
        } else {
          this.appGlobalService.showToast( res.message, 1000, 'middle');
        }
      });
  }
  // css拼接
  getCss(obj): any {
    if (obj) {
      const cssStyles = obj.split(";");
      const childCss = {};
      // tslint:disable-next-line:prefer-for-of
      for (let i = 0; i < cssStyles.length; i++) {
        const cssStrs = cssStyles[i].split(":");
        childCss[cssStrs[0]] = cssStrs[1];
      }
      return childCss;
    } else {
      return null;
    }
  }

  // 拨打号码
  async callConsultantNow(data) {
    const alert = await this.alertController.create({

      header: '',
      subHeader: '',
      message: data,
      buttons: [{
        text: '取消',
        role: 'cancel',
        cssClass: 'secondary',
        handler: () => {
          console.log('取消');
        }
      }, {
        text: '拨打',
        cssClass: 'LJ',
        handler: () => {
          this.http.changeCallNumber(data);
        }
      }]
    });
    await alert.present();
  }

  // 滚动查询
  loadData(event) {
    setTimeout(() => {

      this.content.pageNo = ++this.pageNo;
      this.content.pageSize = this.tagData.data.pageSize;
      const params = {
        content: this.content,
        id: this.tagData.data.listKey,
        sessionId: localStorage.getItem("sessionId")
      };
      let items = [];
      this.http.post("application/submit", params, res => {
        items = res.data.data || [];
        if (items.length <= 0) {
          --this.pageNo;
          // App logic to determine if all data is loaded
          // and disable the infinite scroll
          event.target.disabled = true;
          this.appGlobalService.showToast('没有数据了...', 1000, 'bottom');
        } else {
          this.listData = [...this.listData, ...items];
        }
        event.target.complete();
      }, () => {
        --this.pageNo;
        event.target.complete();
      });
    }, 500);
  }

}
